﻿var common_isAndroid = (/android/gi).test(navigator.appVersion);
var common_isIDevice = (/iphone|ipad/gi).test(navigator.appVersion);
var common_isPlaybook = (/playbook/gi).test(navigator.appVersion);
var common_isTouchPad = (/hp-tablet/gi).test(navigator.appVersion);

var common_isMobile = common_isAndroid || common_isIDevice;

var common_isWX = (/MicroMessenger/gi).test(navigator.appVersion);

var common_hasTouch = 'ontouchstart' in window && !common_isTouchPad;
var common_touchstartEventName = common_hasTouch ? 'touchstart' : 'mousedown';
var common_touchendEventName = common_hasTouch ? 'touchend' : 'mouseup';
var common_touchmoveEventName = common_hasTouch ? 'touchmove' : 'mousemove';

var jq_ready = false;
var wx_ready = false;


function func_confirm(str)
{
    if (str)
    {
        //pass
    }
    else
    {
        str = "确定吗？"
    }

    return confirm(str);
}

function func_alert(msg)
{
    alert(msg);
}


//////////////////////////////////////////////////
////// 加载中
function func_loading(is_hide, content)
{
    var loading = document.getElementById('wx_loading');
    if (loading == null)
    {
        var div = document.createElement("div");
        div.setAttribute("id", "wx_loading");
        div.setAttribute("class", "x_hide");
        div.innerHTML = `
			<div class="weui-loading_toast custom-loading">
			<div class="weui-mask_transparent"></div>
			<div class="weui-toast weui-animate-fade-in">
				<i class="weui-loading weui-icon_toast"></i>
				<p class="weui-toast__content">加载中</p>
			</div>
		</div>
			`;

        document.body.appendChild(div);
        loading = document.getElementById('wx_loading');
    }

    if (typeof (content) != 'undefined')
    {
        if (content != "加载中")
        {
            loading.innerHTML = loading.innerHTML.replace("加载中", content);
        }
    }

    if (typeof (is_hide) != 'undefined')
    {
        loading.setAttribute("class", "x_hide");
    }
    else
    {
        loading.setAttribute("class", "x_show");
    }
}


//////////////////////////////////////////////////
/// 日志页面最下方显示
function func_api_log(content)
{
    if ($("#api_errmsg").length > 0)
    {
        console.log("api_errmsg 存在");
    }
    else
    {
        $("body").append("<div id='api_errmsg' class='api_errmsg'></div>");
        $("#api_errmsg").css("width", window.screen.width + "px");
    }


    $("#api_errmsg").show();
    $("#api_errmsg").html(content);
    $("#api_errmsg").css("top", window.screen.height - $("#api_errmsg").height() + "px");
}

//////////////////////////////////////////////////
/// 日志隐藏
function func_api_log_hide()
{
    $("#api_errmsg").hide();
}


//////////////////////////////////////////////////
////// 返回
function func_return()
{
    history.go(-1);
}


if (typeof (wx) != 'undefined')
{
    wx.ready(function ()
    {
        g_wx_ready = true;
    });
}

function func_jq_ready(func)
{
    console.log("jq_ready");

    if (jq_ready)
    {
        func();
    }
    else
    {
        $(func);
    }
}


function func_wx_ready(func)
{
    console.log("wx_ready");

    if (common_isWX)
    {
        if (wx_ready)
        {
            func();
        }
        else
        {
            wx.ready(func);
        }
    }
}


//////////////////////////////////////////////////
////// 获取url参数的值
function func_request_get(key)
{
    var request_str = window.location.search;
    request_str = request_str.substr(1);

    var request_data = request_str.split('&');

    var val = "";
    for (var i = 0; i < request_data.length; i++)
    {
        var target_data = request_data[i].split('=');
        if (target_data[0] == key)
        {
            val = target_data[1];

            break;
        }
    }

    return val;
}


$(function ()
{
    jq_ready = true;


    //////////////////////////////////////////////////
    ////// ajax通用配置
    $.ajaxSetup(
        {
            cache: false,
            ////////////////////////////////////////////////
            beforeSend: function (XHR)
            {
                setTimeout(function ()
                {
                    func_loading();
                }, 10);
            },
            //////////////////////////////////////////////////
            complete: function (XHR, TS)
            {
                setTimeout(function ()
                {
                    func_loading(true);
                }, 10);
            },
            error: function (data, textStatus, XHR)
            {
                //console.log(data.responseText);
                console.log($(data.responseText).find('.error-box').text());


                var common_errmsg = $(data.responseText).find('.error-box').text();
                var common_errmsg_s = common_errmsg.split('\\');
                common_errmsg = common_errmsg_s[common_errmsg_s.length - 1];

                var font_size = common_errmsg.length;
                if (font_size > 45)
                {
                    font_size = 45;
                }
                common_errmsg = common_errmsg.substring(0, font_size);


                var errmsg = "errcode:500<br/>errmsg:{#statusText}"
                    .replace("{#statusText}", common_errmsg);

                func_api_log(errmsg);
            }
        });
})


//////////////////////////////////////////////////
////// ajax
function func_ajax(obj)
{
    if (typeof (obj.url) == "undefined")
    {
        obj.url = "api__api.aspx";
    }

    if (typeof (obj.error) == "undefined")
    {
    	obj.error = function (res)
    	{
    		func_alert("系统繁忙，稍后再试");
    	}
    }

    obj.dataType = "json";


    ////////////////////参数///////////////////////
    if (typeof (obj.data.__d) == "undefined")
    {
        obj.data.__d = (new Date()).valueOf();
    }
    ////////////////////参数///////////////////////

    $.ajax(obj);
}


/**
 * 获取URL，?后面的值
 * @param paraName
 * @returns {string}
 */
function func_get_url_param(paraName)
{
    var url = document.location.toString();
    var arrObj = url.split("?");

    if (arrObj.length > 1)
    {
        var arrPara = arrObj[1].split("&");
        var arr;

        for (var i = 0; i < arrPara.length; i++)
        {
            arr = arrPara[i].split("=");
            if (arr != null && arr[0] == paraName)
            {
                return arr[1];
            }
        }
        return "";
    }
    else
    {
        return "";
    }
}